package com.wtwd.campus.service.impl;


import com.wtwd.campus.constant.UploadAppFileTypeEnum;
import com.wtwd.campus.dao.DeviceAppPublicFileMapper;
import com.wtwd.campus.entity.DeviceAppPublicFile;
import com.wtwd.campus.entity.MqAppFile;
import com.wtwd.campus.entity.MqPhoneDatas;
import com.wtwd.campus.entity.MqUplocadAppFile;
import com.wtwd.campus.model.dto.PackageNameAndTypeDto;
import com.wtwd.campus.receiver.IReceiverHandler;
import com.wtwd.campus.service.DeviceAppPublicFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wtwd.campus.utils.FileUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2022-12-12
 */
@Service
public class DeviceAppPublicFileServiceImpl extends ServiceImpl<DeviceAppPublicFileMapper, DeviceAppPublicFile> implements DeviceAppPublicFileService {


    @Resource
    private DeviceAppPublicFileMapper deviceAppPublicFileMapper;

    @Value("${app.upload.file}")
    private String appUploadFile;


    @Override
    public List<DeviceAppPublicFile> queryDeviceAppPublicFileByPackageNameAndTypeDto(List<PackageNameAndTypeDto> packageNameAndTypeDtos) {
        if (packageNameAndTypeDtos == null || packageNameAndTypeDtos.size() == 0) {
            return null;
        }
        return deviceAppPublicFileMapper.queryDeviceAppPublicFileByPackageNameAndTypeDto(packageNameAndTypeDtos);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insertOrUpdate(List<DeviceAppPublicFile> deviceAppPublicFile) {
        if (deviceAppPublicFile == null || deviceAppPublicFile.size() == 0) {
            return false;
        }
        return deviceAppPublicFileMapper.insertOrUpdate(deviceAppPublicFile);
    }

    @Override
    public Boolean bachCacheSaveFile(MqUplocadAppFile uploadAppFile,long uploadTime) throws IOException {
        if (uploadAppFile == null && uploadAppFile.getAppFiles().size() == 0) {
            return false;
        }
        //做更新或者插入的数据
        List<DeviceAppPublicFile> deviceAppPublicFiles = new ArrayList<>();
        List<MqAppFile> appFiles = uploadAppFile.getAppFiles();
        for (MqAppFile appFile : appFiles) {
            DeviceAppPublicFile deviceAppPublicFile = new DeviceAppPublicFile();
            deviceAppPublicFile.setPackageName(appFile.getPackageName());
            deviceAppPublicFile.setCreateTime(new Date().getTime());
            deviceAppPublicFile.setUpdateTime(new Date().getTime());
            deviceAppPublicFile.setUploadTime(uploadTime);
            deviceAppPublicFile.setAppPublicFileType(uploadAppFile.getType());
            deviceAppPublicFiles.add(deviceAppPublicFile);
        }
        deviceAppPublicFiles = contrastCacheOverdueData(deviceAppPublicFiles);
        //进行文件上传,并且插入或者更新
        for (DeviceAppPublicFile deviceAppPublicFile : deviceAppPublicFiles) {
            for (MqAppFile appFile : appFiles) {
                if (deviceAppPublicFile.getPackageName().equals(appFile.getPackageName())) {
                    String path = appUploadFile.concat(String.valueOf(new Date().getTime())).concat(appFile.getFileName());
                    FileUtil.base64ToFile(appFile.getFile(), path);
                    deviceAppPublicFile.setFilePath(path);
                    continue;
                }
            }
        }
        return insertOrUpdate(deviceAppPublicFiles);
    }

    @Override
    public List<DeviceAppPublicFile> contrastCacheOverdueData(List<DeviceAppPublicFile> deviceAppPublicFiles) {
        //查询查询距离上次文件更新文件时间是否超过缓存时间,超过则重新上传文件,更新数据状态
        List<PackageNameAndTypeDto> packageNameAndTypeDtos = new ArrayList<>();
        deviceAppPublicFiles.stream().forEach(e->{
            PackageNameAndTypeDto packageNameAndTypeDto = new PackageNameAndTypeDto();
            packageNameAndTypeDto.setPackageName(e.getPackageName());
            packageNameAndTypeDto.setType(e.getAppPublicFileType());
            packageNameAndTypeDtos.add(packageNameAndTypeDto);
        });
        List<DeviceAppPublicFile> dataDeviceAppPublicFile = deviceAppPublicFileMapper.queryDeviceAppPublicFileByPackageNameAndTypeDto(packageNameAndTypeDtos);
        if (dataDeviceAppPublicFile != null && dataDeviceAppPublicFile.size() > 0) {
            deviceAppPublicFiles = deviceAppPublicFiles.stream().filter(e -> {
                for (DeviceAppPublicFile deviceAppPublicFile : dataDeviceAppPublicFile) {
                    if (e.getPackageName().equals(deviceAppPublicFile.getPackageName()) &&
                            e.getAppPublicFileType().equals(deviceAppPublicFile.getAppPublicFileType())
                            && e.getUploadTime() <
                            (deviceAppPublicFile.getUploadTime() + 1000 * 60 * UploadAppFileTypeEnum.getUploadAppFileTypeEnumByType(deviceAppPublicFile.getAppPublicFileType()).getCacheTime())) {
                        return false;
                    }
                }
                return true;
            }).collect(Collectors.toList());
        }
        return deviceAppPublicFiles;
    }

    public static void main(String[] args) throws Exception {
        String image = FileUtil.convertBase64(new File("F:\\AppHeadImg\\1.jpeg"));
        System.out.println(image);
    }

}
